package com.japp.manager.service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.japp.base.BaseService;
import com.japp.manager.bean.EngageInBean;
import com.japp.manager.mapper.EngageInMapper;
import com.japp.tools.common.JappLog;
import com.japp.tools.page.Page;

/**
 * 研究报告
 * 
 * @author Administrator
 * 
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EngageInService implements BaseService {
	@Autowired
	private EngageInMapper engageInMapper;

	public EngageInBean get(Serializable pk) {
		return null;
	}

	public Page<EngageInBean> getPage(Page<EngageInBean> po, EngageInBean obj) {
		try {
			Page<EngageInBean> page = engageInMapper.getPage(po, obj);
			return page;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}

	/**
	 * 根据条件查询
	 * @param en
	 * @return
	 */
	public EngageInBean findEngageInByBean(EngageInBean en){
		try {
			return engageInMapper.findEngageInByBean(en);
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}
	
	public List<EngageInBean> queryEngageInAll() {
		try {
			return engageInMapper.queryEngageInAll();
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}

	public EngageInBean findEngageInById(int enId) {
		try {
			return engageInMapper.findEngageInById(enId);
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return null;
	}

	public int insertEngageIn(EngageInBean en) {
		try {
			en = formatEi(en);
			int rel = engageInMapper.insertEngageIn(en);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

	public int deleteEngageIn(String[] keys) {
		try {
			int rel = engageInMapper.deleteEngageIn(keys);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

	public int updateEngageIn(EngageInBean en) {
		try {
			en = formatEi(en);
			int rel = engageInMapper.updateEngageIn(en);
			return rel;
		} catch (Exception e) {
			JappLog.log.error("", e);
		}
		return 0;
	}

	private EngageInBean formatEi(EngageInBean e) {
		String pwoer = e.getEn_power();
		e.setEn_power((pwoer!=null&&"0".equals(pwoer)?"1":pwoer));
		//double EN_CWLXJSR = e.getEn_cwlxjsr();
		//5.财务利息净收入(可用资金*1%)
		e.setEn_cwlxjsr(e.getEn_kyzj().multiply(BigDecimal.valueOf(0.01)));
		//11.收入计划合计(5).add(7+8+9+10)
		e.setEn_sum_srjh(e.getEn_cwlxjsr()
				.add(e.getEn_sxfsr())
				.add(e.getEn_jmfhjsr())
				.add(e.getEn_yjjsr())
				.add(e.getEn_srother()));
		//16.职工部分合计(13+14+15)
		e.setEn_sum_zg(e.getEn_zggz()
				.add(e.getEn_zgjx())
				.add(e.getEn_zgjj()));
		//18.工会经费(16*2%)
		e.setEn_sum_jf(e.getEn_sum_zg().multiply(BigDecimal.valueOf(0.02)));
		//19.职工教育经费(16*2.5%)
		e.setEn_sum_jyjf(e.getEn_sum_zg().multiply(BigDecimal.valueOf(0.025)));
		//28.办公合计(25+26+27)
		e.setEn_sum_bg(e.getEn_rcbg()
				.add(e.getEn_zxbgf())
				.add(e.getEn_ydf()));
		//34.物业水电费合计(32+33)
		e.setEn_sum_wy(e.getEn_wygl().add(e.getEn_sdf()));
		//42.住房公积金及租房补合计(40+41)
		e.setEn_sum_zfgjjzfbt(e.getEn_zfgjj().add(e.getEn_zfbt()));
		//45.管理费用计划小计(3-45(子项)
		e.setEn_sum_glfy(
				/*e.getEn_khqy()+		//3
				e.getEn_kyzj()+		//4
				e.getEn_jye()+		//6
				e.getEn_sxfsr()+	//7
				e.getEn_jmfhjsr()+	//8
				e.getEn_yjjsr()+	//9
				e.getEn_srother()+	//10*/
				e.getEn_zggz().add(		//13
				e.getEn_zgjx()).add(		//14
				e.getEn_zgjj()).add(		//15
				e.getEn_fl()).add(		//17
				e.getEn_shbx()).add(		//20
				e.getEn_ldbh()).add(		//21
				e.getEn_ywzd()).add(		//22
				e.getEn_txf()).add(		//23
				e.getEn_clf()).add(		//24
				e.getEn_rcbg()).add(		//25
				e.getEn_zxbgf()).add(	//26
				e.getEn_ydf()).add(		//27
				e.getEn_jtf()).add(		//29
				e.getEn_hyf()).add(		//30
				e.getEn_zpf()).add(		//31
				e.getEn_wygl()).add(		//32
				e.getEn_sdf()).add(		//33
				e.getEn_zjf()).add(		//35
				e.getEn_xlf()).add(		//36
				e.getEn_wxzc()).add(		//37
				e.getEn_fshbx()).add(	//38
				e.getEn_xxhf()).add(		//39
				e.getEn_zfgjj()).add(	//40
				e.getEn_zfbt()).add(		//41
				e.getEn_vip()).add(		//43
				e.getEn_zcother())	//44
		);
		//46.主营业务税金及附加(7(计划金额)*5.6%)
		e.setEn_sum_yys(e.getEn_sxfsr().multiply(BigDecimal.valueOf(0.056)));
		//47.客户风险准备金(7*5%)
		e.setEn_sum_khfx(e.getEn_sxfsr().multiply(BigDecimal.valueOf(0.05)));
		//48.营业费用计划合计(47（计划金额）+46（计划金额）)
		e.setEn_plan_yyf(e.getEn_sum_yys().add(e.getEn_sum_khfx()));
		//49.营业利润计划合计(11（计划金额）-48(计划金额))
		e.setEn_plan_lr(e.getEn_sum_srjh().subtract(e.getEn_plan_yyf()));
		//50.可控费用计划合计(16+17+22+23+24+28+29+30)
		e.setEn_plan_kkfy(e.getEn_sum_zg().add(e.getEn_fl()).add(e.getEn_ywzd()).add(e.getEn_txf()).add(e.getEn_clf()).add(e.getEn_sum_bg()).add(e.getEn_jtf()).add(e.getEn_hyf()));
		return e;
	}

}
